﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace KnowledgeAssistant
{
    class GroupUtil
    {
        public static List<Group> GetAllGroups()
        {
            List<Group> groups = new List<Group>() { };
            DataTable groupstable = new DataTable();

            groupstable = DatabaseConnect.execSP("GetAllGroups").Tables[0];

            Group g = null;

            int group_id;
            string group_number;
            string group_name;

            byte course;

            foreach (DataRow r in groupstable.Rows)
            {
                group_id = (int)r["group_id"];
                group_number = r["group_number"].ToString();
                group_name = r["name"].ToString();
                course = (byte)r["course"];

                g = new Group(group_id,group_name,group_number,course);
                groups.Add(g);
            }

            return groups;
        }

        public static Group GetGroup(int group_id)
        {
            Group g = null;
            DataTable table = DatabaseConnect.execSP("GetGroup", new List<string>() { group_id.ToString() }).Tables[0];
            
            int groupid = (int)(table.Rows[0]["group_id"]);
            string group_name = table.Rows[0]["name"].ToString();
            string group_number = table.Rows[0]["group_number"].ToString();

            byte course = byte.Parse(
                ((table.Rows[0]["course"].ToString() == "") ? "0" : table.Rows[0]["course"].ToString())
                );

            g = new Group(group_id,group_name,group_number,course);
            return g;
        }

        public static void AddGroupNumber(Group g) 
        {
            if (g.GroupID == 0)
            {
                List<string> parameters = new List<string>() { };
                parameters.Add(g.GroupName);
                parameters.Add(g.GroupNumber);

                DatabaseConnect.execSPNonQuery("AddGroup", parameters);
            }
        }

        public static void EditGroupNumber(Group g, string new_group_number, string new_group_name) 
        {
            if (g.GroupID != 0)
            {
                List<string> parameters = new List<string>() { };
                parameters.Add(g.GroupID.ToString());
                parameters.Add(new_group_name);
                parameters.Add(new_group_number);

                DatabaseConnect.execSPNonQuery("UpdateGroup", parameters);
            }
        }

        public static void DeleteGroup(Group g)
        {
            List<string> parameters = new List<string>() { };
            parameters.Add(g.GroupID.ToString());
            DatabaseConnect.execSPNonQuery("DeleteGroup", parameters);
        }
    }
}
